Imports System.Data.OracleClient

Namespace persistent
    Public Class LegalContactInformation

#Region "Primary Methods"

        Public Shared Function GetInformation(ByVal custCode As String) As DataTable

            Dim query As String = String.Format("select * from eu_bill.legal_contact_information where custcode = '{0}'", custCode)

            Return persistent.myDBConn.myORATable(query)
        End Function

        Public Shared Function SetInformation _
        ( _
            ByVal custcode As String, _
            ByVal firstName As String, _
            ByVal lastName As String, _
            ByVal title As String, _
            ByVal tcID As String, _
            ByVal birthDate As String, _
            ByVal birthPlace As String, _
            ByVal idtypeCode As String, _
            ByVal idNo As String, _
            ByVal businessPhone As String, _
            ByVal phone As String, _
            ByVal fax As String, _
            ByVal email As String, _
            ByVal mernisCheckStatus As String, _
            ByRef resultMessage As String _
        ) As Boolean

            Dim procedureName As String = "eu_bill.pkg_legal_contact_information." & IIf(Exists(custcode), "update_contact", "insert_contact")
            Dim parameterNames() As String = New String() {"p_custcode", "p_first_name", "p_last_name", "p_title", "p_tcid", "p_birthdate", "p_birth_place", "p_idtype_code", "p_id_no", "p_business_phone", "p_phone", "p_fax", "p_email", "p_mernis_check_status"}
            Dim parameterValues() As String = New String() {custcode, firstName, lastName, title, tcID, birthDate, birthPlace, idtypeCode, idNo, businessPhone, phone, fax, email, mernisCheckStatus}
            Dim parameterTypes() As OracleClient.OracleType = New OracleClient.OracleType() {OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.DateTime, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar}

            resultMessage = String.Empty
            Try
                persistent.myDBConn.ORAProcedure(procedureName, parameterNames, parameterValues, parameterTypes)
                resultMessage = "Kontakt bilgileri kaydedildi."
                Return True
            Catch ex As Exception
                resultMessage = ex.Message
                Return False
            End Try

        End Function

        Public Shared Function DeleteInformation(ByVal custCode As String, ByRef resultMessage As String) As Boolean
            Try
                Return persistent.myDBConn.ORAExecute(String.Format("delete from eu_bill.legal_contact_information where custcode = '{0}'", custCode)) > 0

            Catch ex As Exception
                resultMessage = ex.Message
                Return False
            End Try
        End Function

        Public Shared Function Exists(ByVal custCode As String) As Boolean
            Return persistent.myDBConn.ORAScalar(String.Format("select count(*) from eu_bill.legal_contact_information where custcode = '{0}'", custCode)) > 0
        End Function


        Public Shared Function GetIDTypes() As DataTable
            Return persistent.myDBConn.myORATable("select idtype_code, idtype_name from id_type order by 1")
        End Function

        Public Shared Function CheckMernisStatus(ByVal tcID As String, ByRef name As String, ByRef surname As String) As Object

            Try

                Dim requestBody As String = String.Format("<?xml version='1.0' encoding='UTF-8'?> " & _
                                                vbNewLine & "<IWIS_IN_DATA_UNIT> " & _
                                                vbNewLine & "<IWIS_IN_HEADER version='1.0'> " & _
                                                vbNewLine & "<SYSTEM>OTONOM</SYSTEM> " & _
                                                vbNewLine & "<SERVICE>MERNIS_GET_CUSTOMER_BY_ID</SERVICE> " & _
                                                vbNewLine & "</IWIS_IN_HEADER> " & _
                                                vbNewLine & "<IWIS_SERVICE_PARAMS> " & _
                                                vbNewLine & "<TYPE>1</TYPE> " & _
                                                vbNewLine & "<NATIONALID>{0}</NATIONALID> " & _
                                                vbNewLine & "</IWIS_SERVICE_PARAMS> " & _
                                                vbNewLine & "</IWIS_IN_DATA_UNIT>", _
                                                tcID)

                Dim serviceAddress As String = persistent.myDBConn.ORAScalar("select path from BILLINTRA.BILLINGNET_IWIS_CALL_IP_CONFIG")
                Dim data() As Byte = System.Text.Encoding.Convert(System.Text.Encoding.GetEncoding("windows-1254"), System.Text.Encoding.GetEncoding("iso-8859-9"), System.Text.Encoding.GetEncoding("windows-1254").GetBytes(requestBody))
                Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(serviceAddress)
                request.Method = "POST"
                request.ContentType = "content=""text/xml;charset=iso-8859-9"""
                request.ContentLength = data.Length
                Dim streamWriter As New System.IO.StreamWriter(request.GetRequestStream(), System.Text.Encoding.GetEncoding("iso-8859-9"))

                streamWriter.Write(System.Text.Encoding.GetEncoding("iso-8859-9").GetString(data))
                streamWriter.Close()


                Dim response As System.IO.Stream = request.GetResponse().GetResponseStream()
                Dim streamReader As New IO.StreamReader(response, System.Text.Encoding.GetEncoding("iso-8859-9"))
                Dim responseContent As String = streamReader.ReadToEnd()
                Dim upperedResponseContent As String = responseContent.ToUpper()

                Dim resultCode As String = responseContent.Substring _
                ( _
                    upperedResponseContent.IndexOf("<IWIS_RETURN_CODE>") + "<IWIS_RETURN_CODE>".Length, _
                    upperedResponseContent.IndexOf("</IWIS_RETURN_CODE>") - (upperedResponseContent.IndexOf("<IWIS_RETURN_CODE>") + "<IWIS_RETURN_CODE>".Length) _
                )

                Dim _name As String = name, _surname As String = surname

                If resultCode = "0" Then
                    name = responseContent.Substring _
                    ( _
                        upperedResponseContent.IndexOf("<NAME>") + "<NAME>".Length, _
                        upperedResponseContent.IndexOf("</NAME>") - (upperedResponseContent.IndexOf("<NAME>") + "<NAME>".Length) _
                    )
                    surname = responseContent.Substring _
                    ( _
                        upperedResponseContent.IndexOf("<SURNAME>") + "<SURNAME>".Length, _
                        upperedResponseContent.IndexOf("</SURNAME>") - (upperedResponseContent.IndexOf("<SURNAME>") + "<SURNAME>".Length) _
                    )

                End If

                If (Not name Is Nothing) AndAlso name.Trim().ToLower() = "null" Then
                    If Not _name Is Nothing Then
                        name = _name
                    End If

                    If Not _surname Is Nothing Then
                        name = _surname
                    End If

                    resultCode = "-1"
                End If

                If resultCode <> "0" Then
                    resultCode = "-1"
                End If

                Return resultCode
            Catch ex As Exception
                Return -1
            End Try

        End Function
#End Region


    End Class
End Namespace